//
//  InvoiceVC.m
//  saler-ios
//
//  Created by 王亚陆 on 17/9/12.
//  Copyright © 2017年 sayimba. All rights reserved.
//

#import "InvoiceVC.h"
#import "CustomTextField.h"

@interface InvoiceVC ()
{
    UIImageView * nav;
    NSMutableDictionary * dataDic;

}
@property (nonatomic,strong) UIImageView * backView1;
@property (nonatomic,strong) UIImageView * backView2;
@property (nonatomic,strong) CustomTextField * field1;
@property (nonatomic,strong) CustomTextField * field2;
@property (nonatomic,strong) UILabel * Titlelabel;
@property (nonatomic,strong) UIButton * confirmBtn;

@property (nonatomic,strong) UILabel * headLabel;

@end

@implementation InvoiceVC

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.dicSouce = [[NSMutableDictionary alloc]init];
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    nav = [self createNav:@"发票"];
    [self.view addSubview:nav];
    self.view.backgroundColor = UIviewBackColor;
    
    
    //isTicket
//    @ApiModelProperty(value = "发票抬头")
//    private String companyName;
//    @ApiModelProperty(value = "纳税人识别码")
//    private String identifyNumber;

    
    
    
    dataDic = [[NSMutableDictionary alloc]init];
    
    
    
//    [invoice.dicSouce setObject:postDic[@"isTicket"] forKey:@"isTicket"];
//    [invoice.dicSouce setObject:postDic[@"companyName"] forKey:@"companyName"];
//    [invoice.dicSouce setObject:postDic[@"identifyNumber"] forKey:@"identifyNumber"];

    
    NSString * isTicket = [NSString stringWithFormat:@"%@",self.dicSouce[@"isTicket"]];
    
    if ([isTicket isEqualToString:@"1"]) {
        [dataDic setObject:@"1" forKey:@"isTicket"];
    }else{
        [dataDic setObject:@"0" forKey:@"isTicket"];
    }

    
    [dataDic setObject:@"" forKey:@"companyName"];
    [dataDic setObject:@"" forKey:@"identifyNumber"];

    
    
    
    self.backView1 = [[UIImageView alloc]init];
    self.backView1.backgroundColor = WhiteColor;
    self.backView1.userInteractionEnabled = YES;
    [self.view addSubview:self.backView1];
    
    [self.backView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).with.offset(10*kWidth);
        make.top.mas_equalTo(nav.mas_bottom).with.offset(10*kHeight);
        make.size.mas_equalTo(CGSizeMake(Main_Width-20*kWidth, 100*kHeight));
    }];

    
    UILabel * label1 = [UILabel createUIlabel:@"是否开票" andFont:FontOfSize14 andColor:carScrollColor];
    [self.backView1 addSubview:label1];
    [label1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.backView1.mas_left).with.offset(23*kWidth);
        make.top.mas_equalTo(self.backView1.mas_top).with.offset(20*kHeight);
    }];
    
    

    UIButton * InvoiceYesBtn = [self createBtn:@"不开票" andBorderColor:YellowColor andFont:FontOfSize11 andTitleColor:YellowColor andBackColor:WhiteColor];
    InvoiceYesBtn.tag = 100;

    [InvoiceYesBtn addTarget:self action:@selector(pressBigOrSmall:) forControlEvents:UIControlEventTouchUpInside];
    [self.backView1 addSubview:InvoiceYesBtn];
    [InvoiceYesBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.backView1.mas_left).with.offset(23*kWidth);
        make.top.mas_equalTo(label1.mas_bottom).with.offset(17*kHeight);
        make.size.mas_equalTo(CGSizeMake(52*kWidth, 23*kHeight));
    }];
    UIButton *  InvoiceNoBtn= [self createBtn:@"开票" andBorderColor:LineGrayColor andFont:FontOfSize11 andTitleColor:littleBlackColor andBackColor:WhiteColor];
    [self.backView1 addSubview:InvoiceNoBtn];
    InvoiceNoBtn.tag = 200;
    [InvoiceNoBtn addTarget:self action:@selector(pressBigOrSmall:) forControlEvents:UIControlEventTouchUpInside];
    [InvoiceNoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(InvoiceYesBtn.mas_right).with.offset(10*kWidth);
        make.centerY.mas_equalTo(InvoiceYesBtn.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(52*kWidth, 23*kHeight));
    }];

    
    
    
    
    self.headLabel = [UILabel createUIlabel:@"发票抬头" andFont:FontOfSize14 andColor:carScrollColor];
    
    [self.view addSubview:self.headLabel];
    [self.headLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(label1.mas_left);
        make.top.mas_equalTo(self.backView1.mas_bottom).with.offset(10*kHeight);
    }];

    
    
    self.backView2 = [[UIImageView alloc]init];
    self.backView2.userInteractionEnabled = YES;

    self.backView2.backgroundColor = WhiteColor;
    [self.view addSubview:self.backView2];
    [self.backView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left).with.offset(10*kWidth);
        make.top.mas_equalTo(self.backView1.mas_bottom).with.offset(40*kHeight);
        make.size.mas_equalTo(CGSizeMake(Main_Width-20*kWidth, 200*kHeight));
    }];
    
    self.Titlelabel = [UILabel createUIlabel:@"请填写公司名称" andFont:FontOfSize14 andColor:YellowColor];
    [self.backView2 addSubview:self.Titlelabel];
    [self.Titlelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(label1.mas_left);
        make.top.mas_equalTo(self.backView2.mas_top).with.offset(20*kHeight);
    }];
    
    _field1 =[self createField:@"请填写公司名称" andTag:1 andFont:FontOfSize13];
    _field1.placeholder = @"请填写公司名称";
    [_field1 addTarget:self action:@selector(textFieldWithText:) forControlEvents:UIControlEventEditingChanged];
    
    NSString * nameStr = [NSString stringWithFormat:@"%@",self.dicSouce[@"companyName"]];
    
    if (nameStr.length > 0) {
        _field1.text = nameStr;
        [dataDic setObject:nameStr forKey:@"companyName"];
    }

    
    
    
    
    [self.backView2 addSubview:_field1];
    LRViewBorderRadius(_field1, 3, 0.5, AddCarNameBtnColor);

    [_field1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.backView2.mas_left).with.offset(10*kWidth);
        make.top.mas_equalTo(self.Titlelabel.mas_bottom).with.offset(10*kHeight);
        make.size.mas_equalTo(CGSizeMake(336*kWidth, 43*kHeight));
    }];

    
    
    UILabel * label4 = [UILabel createUIlabel:@"请在此填写纳税人识别号" andFont:FontOfSize14 andColor:carScrollColor];
    [self.backView2 addSubview:label4];
    [label4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(label1.mas_left);
        make.top.mas_equalTo(_field1.mas_bottom).with.offset(20*kHeight);
    }];

    _field2 =[self createField:@"请在此填写纳税人识别号" andTag:2 andFont:FontOfSize13];
    _field2.placeholder = @"请在此填写纳税人识别号";
    [_field2 addTarget:self action:@selector(textFieldWithText:) forControlEvents:UIControlEventEditingChanged];
    
    NSString * NumberStr = [NSString stringWithFormat:@"%@",self.dicSouce[@"identifyNumber"]];
    
    if (NumberStr.length > 0) {
        _field2.text = NumberStr;
        [dataDic setObject:NumberStr forKey:@"identifyNumber"];
    }
    

    
    LRViewBorderRadius(_field2, 3, 0.5, AddCarNameBtnColor);
    
    [self.backView2 addSubview:_field2];
    [_field2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.backView2.mas_left).with.offset(10*kWidth);
        make.top.mas_equalTo(label4.mas_bottom).with.offset(10*kHeight);
        make.size.mas_equalTo(CGSizeMake(336*kWidth, 43*kHeight));
    }];

    
    

    
    self.confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [self.confirmBtn setTitleColor:WhiteColor forState:UIControlStateNormal];
    [self.confirmBtn addTarget:self action:@selector(pressConfirmBtn) forControlEvents:UIControlEventTouchUpInside];
    LRViewBorderRadius(self.confirmBtn, 5, 0.5, YellowColor);
    self.confirmBtn.backgroundColor = YellowColor;
    [self.view addSubview:self.confirmBtn];
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.size.mas_equalTo(CGSizeMake(355*kWidth, 50*kHeight));
        make.bottom.mas_equalTo(self.view.mas_bottom).with.offset(-170*kHeight);
    }];
    
    
    if ([dataDic[@"isTicket"] isEqualToString:@"0"]) {
        _field1.userInteractionEnabled = NO;
        _field2.userInteractionEnabled = NO;
    }
    
    
    
    if ([isTicket isEqualToString:@"0"]) {
        
        _backView2.hidden = YES;
//        self.confirmBtn.hidden = YES;
        _Titlelabel.hidden = YES;
        _headLabel.hidden = YES;
        
  
    }else{
       
        _backView2.hidden = NO;
//        self.confirmBtn.hidden = NO;
        _Titlelabel.hidden = NO;
        _headLabel.hidden = NO;
        
        InvoiceNoBtn.layer.borderColor = YellowColor.CGColor;
        [InvoiceNoBtn setTitleColor:YellowColor forState:UIControlStateNormal];
        InvoiceYesBtn.layer.borderColor = LineGrayColor.CGColor;
        [InvoiceYesBtn setTitleColor:littleBlackColor forState:UIControlStateNormal];


    }
    

}


-(void)pressConfirmBtn{
    

    
    if ([dataDic[@"isTicket"] isEqualToString:@"1"]) {
        
        
        NSString * companyName = [NSString stringWithFormat:@"%@",dataDic[@"companyName"]];
        NSString * identifyNumber = [NSString stringWithFormat:@"%@",dataDic[@"identifyNumber"]];

        if (companyName.length == 0 ||identifyNumber.length == 0 ) {
            
            [self createUIAlertController:@"请填写公司完整信息"];
        
        }else{
            
            if(_callBackInvoice){
                _callBackInvoice(dataDic);
            }
        }
        
    }else{
        
        if(_callBackInvoice){
            _callBackInvoice(dataDic);
        }

    }
    
    [self.navigationController popViewControllerAnimated:YES];

    
    
    

    
    NSLog(@"下一步");
    
    
    
}



-(void)pressBigOrSmall:(UIButton*)sender{
    
    UIButton * big = (UIButton*)[self.view viewWithTag:100];
    UIButton * small = (UIButton*)[self.view viewWithTag:200];
    if (sender.tag == 100) {
        big.layer.borderColor = YellowColor.CGColor;
        [big setTitleColor:YellowColor forState:UIControlStateNormal];
        small.layer.borderColor = LineGrayColor.CGColor;
        [small setTitleColor:littleBlackColor forState:UIControlStateNormal];
        [dataDic setObject:@"0" forKey:@"isTicket"];
        
        _field1.text = @"";
        _field2.text = @"";
        
        [dataDic setObject:@"" forKey:@"companyName"];
        [dataDic setObject:@"" forKey:@"identifyNumber"];

        _field1.userInteractionEnabled = NO;
        _field2.userInteractionEnabled = NO;


        _backView2.hidden = YES;
//        self.confirmBtn.hidden = YES;
        _Titlelabel.hidden = YES;
        _headLabel.hidden = YES;

       
        
        
    }else{
        small.layer.borderColor = YellowColor.CGColor;
        [small setTitleColor:YellowColor forState:UIControlStateNormal];
        big.layer.borderColor = LineGrayColor.CGColor;
        [big setTitleColor:littleBlackColor forState:UIControlStateNormal];
        [dataDic setObject:@"1" forKey:@"isTicket"];
        _field1.userInteractionEnabled = YES;
        _field2.userInteractionEnabled = YES;
        
        _backView2.hidden = NO;
//        self.confirmBtn.hidden = NO;
        _Titlelabel.hidden = NO;
        _headLabel.hidden = NO;


    }

    
    
}



-(void)textFieldWithText:(UITextField *)textField{

    UITextField * field = (UITextField*)textField;
    switch (textField.tag) {
        case 1://公司名称
            if ([self textField:field shouldChangeCharactersInRange:NSMakeRange(0, 0) replacementString:@""]) {
                [dataDic setObject:textField.text forKey:@"companyName"];
            }
            break;
        case 2: //识别号
            if ([self textField:field shouldChangeCharactersInRange:NSMakeRange(0, 0) replacementString:@""]) {
                
                [dataDic setObject:textField.text forKey:@"identifyNumber"];
                
            }
            break;

            
        default:
            break;
    }
    
}



//触摸方法
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{
    for (UITextField * field in self.view.subviews) {
        [field resignFirstResponder];
    }
}

//点击return按钮 键盘隐藏 这是协议中的方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //键盘隐藏 也就是让键盘取消第一响应者身份
    [textField resignFirstResponder];
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;
{  //string就是此时输入的那个字符textField就是此时正在输入的那个输入框返回YES就是可以改变输入框的值NO相反
    
    if ([string isEqualToString:@"\n"])  //按会车可以改变
    {
        return YES;
    }
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到输入框的内容
    
    if (textField.tag == 1) {
        
        if ([toBeString length] >50) {
            [self createUIAlertController:@"公司名称不能超过50个字符"];
            return NO;
        }
    }else if (textField.tag == 2){
        if ([toBeString length] >100) {
            [self createUIAlertController:@"识别号输入不能超过11个字符"];
            return NO;
        }
    }else{
        if ([toBeString length] >50) {
            [self createUIAlertController:@"不能超过50个字符"];
        }
    }
    
    return YES;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UIButton*)createBtn:(NSString*)title andBorderColor:(UIColor*)BorderColor andFont:(CGFloat)Font andTitleColor:(UIColor*)titleColor andBackColor:(UIColor*)BackColor {
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.layer.borderWidth = 0.5;
    btn.layer.cornerRadius = 5;
    btn.backgroundColor = BackColor;
    btn.layer.borderColor = BorderColor.CGColor;
    btn.titleLabel.font = Font(Font);
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:TitleColor forState:UIControlStateNormal];
    return btn;
}

-(void)createUIAlertController:(NSString*)title

{
    UIAlertController * alert =[UIAlertController alertControllerWithTitle:@"提示" message:title preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * action =[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction * action1 =[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:action1];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}

-(CustomTextField*)createField:(NSString*)placeholder andTag:(NSInteger)tag andFont:(double)font{
    
    NSMutableAttributedString * placeH = [[NSMutableAttributedString alloc]initWithString:placeholder];
    
    CustomTextField * field =[[CustomTextField alloc]init];
    field.frame = CGRectMake(84*kWidth, 0, Main_Width-100*kWidth, 43*kWidth);
    field.delegate = self;
    field.font = Font(font);
    field.tag = tag;
    
    [field setValue:[UIFont boldSystemFontOfSize:font]forKeyPath:@"_placeholderLabel.font"];
    
    [placeH addAttribute:NSFontAttributeName
                   value:[UIFont boldSystemFontOfSize:16]
                   range:NSMakeRange(0, placeholder.length)];
    field.attributedPlaceholder = placeH;
    
    field.clearButtonMode = UITextFieldViewModeWhileEditing;
    field.userInteractionEnabled = YES;
    field.textAlignment = NSTextAlignmentLeft;
    field.returnKeyType = UIReturnKeyDone;
    CGFloat width = field.frame.size.width;
    CGFloat height = field.frame.size.height;
    //控制placeHolder的位置
    [field placeholderRectForBounds:CGRectMake(width/2, 0, width, height)];
    
    [field clearButtonRectForBounds:field.frame];
    
    return field;
}

@end
